package com.karaku.karaku_collector.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.karaku.karaku_collector.R;
import com.karaku.karaku_collector.common.KarakuActivityManager;
import com.karaku.karaku_collector.common.binder.BindView;
import com.karaku.karaku_collector.common.binder.OnClick;
import com.karaku.karaku_collector.common.binder.UIBinder;
import com.karaku.karaku_collector.util.actions.Action1;

/**
 * Created by guoping on 2017/10/27.
 */

@BindView(R.layout.view_room_id_edit)
public class RoomIdEdtView extends FrameLayout {

    @BindView(R.id.btn_ok)
    private Button btnOk;
    @BindView(R.id.edt_room_id)
    private EditText edtRoomId;

    private Action1<String> callback;

    public RoomIdEdtView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        UIBinder.bind(this, this, true);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
    }

    public void show(final String roomId, Action1<String> callback) {
        this.callback = callback;
        post(new Runnable() {
            @Override
            public void run() {
                if(!TextUtils.isEmpty(roomId)) {
                    edtRoomId.setText(roomId);
                } else {
                    edtRoomId.setText("");
                }
            }
        });
        this.setVisibility(VISIBLE);
    }

    public void hide() {
        this.callback = null;
        this.edtRoomId.setText("");
        this.setVisibility(GONE);

        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edtRoomId.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    @OnClick(R.id.btn_ok)
    private void ok() {
        if (callback != null) {
            callback.run(edtRoomId.getText().toString());
        }
        hide();
    }
}
